/**
 * 
 */
package com.faxsun.core.service.impl;

import java.util.List;

import mblog.core.persist.dao.FollowListDao;
import mblog.core.persist.dao.TagDao;
import mtons.modules.pojos.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.faxsun.core.persist.entity.FollowListPO;
import com.faxsun.core.persist.entity.ProductRefPO;
import com.faxsun.core.persist.entity.TagPO;
import com.faxsun.core.pojos.enums.FollowStatusType;
import com.faxsun.core.service.FollowListService;
import com.faxsun.core.service.LiveMsgService;

/**
 * @author leima
 *
 */
@Transactional(rollbackFor = { Exception.class })
public class FollowListServiceImpl implements FollowListService {

	private final static String CACHE = "FollowListServiceCache";

	@Autowired
	private FollowListDao followListDao;

	@Autowired
	private TagDao tagDao;
	
	@Autowired
	private LiveMsgService liveMsgService;

	@Override
	// @Cacheable(value = CACHE, key =
	// "'findFollowers?page=' + #page.getPageNo() + '&uid=' + #uid + '&itemType=' + #itemType")
	@Transactional
	public List<FollowListPO> findAllMyFollowers(Page page, long uid,
			int itemType) {
		return followListDao.findAllMyFollowers(page, uid, itemType);
	}

	@Override
	// @Cacheable(value = CACHE, key =
	// "'findFollows?page=' + #page.getPageNo() + '&followerId=' + #followerId + '&itemType=' + #itemType")
	@Transactional
	public List<FollowListPO> findAllMyFollows(Page page, long followerId,
			int itemType) {
		return followListDao.findAllMyFollows(page, followerId, itemType);
	}

	@Override
	// @Cacheable(value = CACHE, key =
	// "'findFollowerCount?uid=' + #uid + '&itemType=' + #itemType")
	@Transactional
	public int countAllMyFollowers(long uid, int itemType) {
		return followListDao.countAllMyFollowers(uid, itemType);
	}

	@Override
	// @Cacheable(value = CACHE, key =
	// "'findFollowCount?followerId=' + #followerId + '&itemType=' + #itemType")
	@Transactional
	public int countAllMyFollows(long followerId, int itemType) {
		return followListDao.countAllMyFollows(followerId, itemType);
	}

	@Override
	@Transactional
	public FollowListPO follow(FollowListPO fl) {
		fl.setStatus(FollowStatusType.FOLLOWED.getIntValue());// in effect
		followListDao.saveOrUpdate(fl);

		// 如果关注的是商品，再商品的关注数上＋1
		if (fl.getItem_type() == 1) {
			TagPO productTag = tagDao.get(fl.getUid());
			ProductRefPO productRefPO = productTag.getProductRef();
			productRefPO.setFavorite(productRefPO.getFavorite() + 1);
			tagDao.save(productTag);
		}
		return fl;
	}

	@Override
	@Transactional
	public FollowListPO unfollow(FollowListPO fl) {
		fl.setStatus(FollowStatusType.UNFOLLOWED.getIntValue());// not in effect
		followListDao.saveOrUpdate(fl);
		// 如果关注的是商品，再商品的关注数上-1
		if (fl.getItem_type() == 1) {
			TagPO productTag = tagDao.get(fl.getUid());
			ProductRefPO productRefPO = productTag.getProductRef();
			productRefPO.setFavorite(productRefPO.getFavorite() - 1);
			tagDao.save(productTag);
		}
		return fl;
	}

	@Override
	@Transactional
	// @Cacheable(value = CACHE, key =
	// "'findFollow?uid=' + #uid + '&followerId=' + #followerId + '&itemType=' + #itemType")
	public FollowListPO findFollow(long uid, long followerId, int itemType) {
		return followListDao.findFollow(uid, followerId, itemType);
	}

	@Override
	@Transactional
	@Cacheable(value = CACHE, key = "'thumbs?uid='+#uid+'&type='+#itemType")
	public int countAllMyThumbsupers(long uid, int itemType) {
		return followListDao.countAllMyThumbsupers(uid, itemType);
	}

	@Override
	@Transactional
    @CacheEvict(value = CACHE, key = "'thumbs?uid='+#fl.uid+'&type='+#fl.item_type")
	public FollowListPO thumbsup(FollowListPO fl) {
		boolean isThumbsup=fl.getThumbsup();
		if(!isThumbsup && fl.getItem_type()==4){
			//TODO: LiveMsg's thumbsup +1
			liveMsgService.incrementThumbup(fl.getUid());
		}
		fl.setThumbsup(true);// in effect
		followListDao.saveOrUpdate(fl);
		return fl;
	}

    @Override
    @Transactional
    @CacheEvict(value = CACHE, key = "'thumbs?uid='+#fl.uid+'&type='+#fl.item_type")
    public FollowListPO thumbsupCancel(FollowListPO fl) {
    	boolean isThumbsup=fl.getThumbsup();
		if(isThumbsup && fl.getItem_type()==4){
			//TODO: LiveMsg's thumbsup -1
			liveMsgService.decrementThumbup(fl.getUid());
		}
        fl.setThumbsup(false);// in effect
        followListDao.saveOrUpdate(fl);
        return fl;
    }

}
